﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SQLMetalTool.cs" company="">
//   
// </copyright>
// <summary>
//   Defines the OutputPipeLine type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace SQLMetalOSUICtrl
{
    using System;
    using System.Reflection;
    using System.Windows.Forms;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="results"></param>
    public delegate void OutputPipeLine(string results);

    /// <summary>
    /// This class represents SQL Metal.exe tool and it properties 
    /// like path where SQLMetal.exe might be found.
    /// </summary>
    public class SQLMetalTool
    {
        /// <summary>
        /// will store sqlmetal.exe path
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// </summary>
        private LineCommandProcess _process;

        /// <summary>
        /// </summary>
        public OutputPipeLine NewLine;

        /// <summary>
        /// </summary>
        private const string Sdk6Path = @"\Microsoft SDKs\Windows\v6.0A\bin\SQLMetal.exe";

        /// <summary>
        /// </summary>
        private const string Sdk71Path = @"\Microsoft SDKs\Windows\v7.1\bin\SQLMetal.exe";

        /// <summary>
        /// </summary>
        private readonly string PfPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
        /// <summary>
        /// Initializes a new instance of the <see cref="SQLMetalTool"/> class. 
        /// Constructor 
        /// </summary>
        public SQLMetalTool()
        {
            if (global::SQLMetalOSUICtrl.Properties.Settings.Default.SQLMetalPath.Length == 0)
            {
                if (System.IO.File.Exists(this.PfPath + Sdk71Path))
                {
                    this.Path = this.PfPath + Sdk71Path;
                }
                else
                {
                    this.Path = this.PfPath + Sdk6Path;
                }
            }
            else
                this.Path = global::SQLMetalOSUICtrl.Properties.Settings.Default.SQLMetalPath;

            // initializing process manager
            this._process = new LineCommandProcess(this);
        }

        /// <summary>
        /// let's generate if we've all parameters ready...
        /// </summary>
        /// <param name="inputFile">
        /// The input File.
        /// </param>
        /// <param name="outputFile">
        /// The output File.
        /// </param>
        public void GenerateDBML(string inputFile, string outputFile)
        {
            NewLine(
                _process.ExecuteWithParameters(
                    string.Format("/dbml:\"{0}\" \"{1}\"", outputFile, inputFile)));

            if (!string.IsNullOrEmpty(outputFile)) MessageBox.Show("File created.", "SqlMetalOSUI");
        }

        /// <summary>
        /// </summary>
        /// <param name="inputFile">
        /// The input file.
        /// </param>
        /// <param name="outputFile">
        /// The output file.
        /// </param>
        /// <param name="namespaceAssigned">
        /// The namespace assigned.
        /// </param>
        public void GenerateCode(string inputFile,
            string outputFile, string namespaceAssigned)
        {
            NewLine(
                _process.ExecuteWithParameters(
                    string.Format("/code:\"{0}\" \"{1}\" /namespace:\"{2}\"",
                    outputFile, inputFile, namespaceAssigned)));


        }

        /// <summary>
        /// </summary>
        /// <param name="inputFile">
        /// The input file.
        /// </param>
        /// <param name="outputMapFile">
        /// The output map file.
        /// </param>
        /// <param name="outputCodeFile">
        /// The output code file.
        /// </param>
        /// <param name="namespaceAssigned">
        /// The namespace assigned.
        /// </param>
        public void GenerateMap(string inputFile, string outputMapFile, string outputCodeFile, string namespaceAssigned)
        {
            NewLine(
                _process.ExecuteWithParameters(
                    string.Format("/map:\"{0}\" /code:\"{1}\" \"{2}\" /namespace:\"{3}\"",
                    outputMapFile, outputCodeFile, inputFile, namespaceAssigned)));

        }


        /// <summary>
        /// Return SqlMetal help information
        /// </summary>
        public void GetHelp()
        {
            //this functions returns sqlmetal helper
            NewLine(_process.ExecuteWithNoParameters());
        }

        public void GetSqlMetalInformation()
        {
            NewLine(_process.ExecuteForInformationPruposes());
        }

        internal void GetStatus()
        {
            NewLine(string.Format("SQL Metal Open Source User Interface " +
                "v.{0} \r\n" +
                "started succesfully at {1}" + Environment.NewLine + "sqlmetal.exe path: {2}",
                this.Version,
                DateTime.Now.ToShortTimeString(),
                this.Path));
        }

        public string Version
        {
            get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); }
        }
    }
}
